﻿using System;
using System.IO;
using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR

namespace HXu.UnityEditor
{
    /// <summary>
    /// 通用工具类
    /// </summary>
    public class CommonUtil
    {
        /// <summary>
        /// 将文本复制到系统剪贴板
        /// </summary>
        /// <param name="str">要复制的文本</param>
        public static void CopyText(string str)
        {
            GUIUtility.systemCopyBuffer = str;
        }
    }

    /// <summary>
    /// 导出工具类
    /// </summary>
    public class Exporter
    {
        /// <summary>
        /// 导出 Unity 包
        /// </summary>
        /// <param name="assetPathName">要导出的资源路径</param>
        /// <param name="fileName">导出的文件名</param>
        public static void ExportPackage(string assetPathName, string fileName)
        {
            try
            {
                AssetDatabase.ExportPackage(assetPathName, fileName, ExportPackageOptions.Recurse);
                Debug.Log($"Successfully exported package: {fileName}");
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to export package: {e.Message}");
            }
        }
    }


    /// <summary>
    /// 导出 Unity 包的编辑器窗口
    /// </summary>
    public class ExportUnityPackage : EditorWindow
    {
        // 菜单项：打开导出 Unity 包的窗口
        [MenuItem("EditorTools(HXu)/3.Export UnityPackage %e", false, 3)]
        private static void CreateWindow()
        {
            //UnityEditorTools_CreateFiles window = ScriptableObject.CreateInstance<UnityEditorTools_CreateFiles>();
            //window.ShowPopup();

            Rect rect = new Rect(0, 0, 500, 500);
            ExportUnityPackage window = GetWindowWithRect<ExportUnityPackage>(rect, false, "Export UnityPackage");
            window.Show();
        }

        private string _name = ""; // Unity 包名称
        private string _path = "Assets/"; // 导出路径

        /// <summary>
        /// 绘制编辑器窗口的 GUI
        /// </summary>
        private void OnGUI()
        {
            EditorGUILayout.LabelField("");

            // 输入 Unity 包名称
            _name = EditorGUILayout.TextField("UnityPackage Name:", _name);
            // 输入导出路径
            _path = EditorGUILayout.TextField("UnityPackage Path:", _path);

            EditorGUILayout.LabelField("");

            // 导出按钮
            if (GUILayout.Button("Export"))
            {
                string packageName = GeneratePackageName(_name); // 生成包名
                string fullPath = $"{_path}\\{packageName}.unityPackage"; // 完整路径

                Exporter.ExportPackage(_path, $"{packageName}.unityPackage");// 导出 Unity 包

                // 将导出的完整路径复制到剪贴板
                CommonUtil.CopyText(fullPath);
                Debug.Log($"Copied path to clipboard: {fullPath}");

                // 打开导出目录
                Application.OpenURL($"file://{Path.Combine(Application.dataPath, "../")}");
                Debug.Log("UnityPackage exported successfully!");
            }
        }

        /// <summary>
        /// 生成 Unity 包名称
        /// </summary>
        /// <param name="name">基础名称</param>
        /// <returns>生成的包名</returns>
        public static string GeneratePackageName(string name)
        {
            return $"{name}_{DateTime.Now:yyyy_MM_dd_HH}"; // 名称 + 当前时间
        }
    }
}

#endif
